package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.QuestionProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;
import com.fynn.fluidlayout.FluidLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/24.
 * 描述：筹款顾问
 */

public class FundraisingAdviserActivity extends BaseActivity<QuestionProtocol> {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.fluid_layout)
    FluidLayout fluidLayout;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private QuestionProtocol mGsonBaseProtocal;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    @Override
    protected void responseData(QuestionProtocol mGsonBaseProtocal) {
        this.mGsonBaseProtocal = mGsonBaseProtocal;
        for (int i = 0; i < mGsonBaseProtocal.getData().size(); i++) {
            QuestionProtocol.DataBean dataBean = mGsonBaseProtocal.getData().get(i);
            TextView textView = (TextView) LayoutInflater.from(this).inflate(R.layout.question_item, null);
            textView.setText(dataBean.getQuestion_title());
            textView.setOnClickListener(new MyClick(i));
            fluidLayout.addView(textView);
            FluidLayout.LayoutParams lp = new FluidLayout.LayoutParams(textView.getLayoutParams());
            lp.setMargins(10, 10, 10, 10);
            textView.setLayoutParams(lp);
        }
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(QuestionProtocol.class);
        requestHttpLoader.setParams(NetConfig.ADVISER_QUESTION, new HashMap<String, String>());
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.fundraising_adviser_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("筹款顾问");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @OnClick(R.id.btn_commit)
    public void onViewClicked() {
        String phone = editPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast("请输入电话号码");
            return;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mGsonBaseProtocal.getData().size(); i++) {
            if (fluidLayout.getChildAt(i).isSelected()) {
                sb.append(mGsonBaseProtocal.getData().get(i).getQuestion_id() + ",");
            }
        }
        String questions = "";
        if (!TextUtils.isEmpty(sb)) {
            questions = sb.substring(0, sb.length() - 1).toString();
        }
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("user_mobile", phone);
        map.put("question_id", questions);
        requestHttpLoader.setParams(NetConfig.ADVISER_COMMIT, map);
        requestHttpLoader.showDialog(this, true, "正在提交");
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                Intent intent = new Intent(FundraisingAdviserActivity.this, CommitSuccessActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    class MyClick implements View.OnClickListener {

        int position;

        public MyClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            TextView text = (TextView) v;
            if (v.isSelected()) {
                text.setBackgroundResource(R.drawable.black_stroke5_bk);
                text.setTextColor(getResources().getColor(R.color.black_text8));
                text.setSelected(false);
            } else {
                text.setBackgroundResource(R.drawable.blue_radiu5_bk);
                text.setTextColor(getResources().getColor(R.color.white));
                text.setSelected(true);
            }
        }
    }
}
